#ifndef TEST_SCREEN_LOD_H
#define TEST_SCREEN_LOD_H

#include <vector>
#include <utility>
#include <iostream>
#include <GL/gl.h>
#include "object3d.h"
#include "vertex.h"
#include "lighting.h"
#include "vect.h"

#include "lods_file_io.h"

class test_screen
{
   public:
   int keyboard_func(unsigned char key, int x, int y);
   void display_func(int width, int height);
   //void mouse_func(int button, int state, int x, int y);
   //void mouse_motion_func(int x, int y);
   void passive_motion_func(int x, int y);

   test_screen(lighting& l, int& polygonMode) :
      light(l), polygon_mode(polygonMode), use_lod(true)
   {
      int ret;
      ret = load_lods("m4",lods);
      if(ret!=0)
      {
         lods.clear();
         rules.clear();
         positions.clear();
         selected_lods.clear();
         ranges.clear();
         transl_to_origin.clear();
      }
      else
      {
         find_ranges();
         find_transl_to_origin();
         ranges.clear();// used only to find translations to the origin
         // rules
         unsigned end = lods.size();
         rules.resize(end);
         for(unsigned i=0; i<end; i++)
            rules[i] = (i+1)*1.0f;
         //for(unsigned i=0; i<end; i++)
            //std::cerr<<rules[i]<<std::endl;

         // user position
         user_pos = vect(0.0f,0.0f,2.0f);
         front = vect(0.0f,0.0f,1.0f);
         hrotv = vect(0.0f,1.0f,0.0f);
         vrotv = vect(1.0f,0.0f,0.0f);
         hrot = 0.0f;
         vrot = 0.0f;
         // positions
         positions.resize(4);
         positions[0] = vect(-0.4,0.0,0.0);
         positions[1] = vect(-0.6,0.0,0.0);
         positions[2] = vect(1.2,0.0,0.0);
         positions[3] = vect(3.0,0.0,0.0);
         // selected_lods
         selected_lods.resize(positions.size());
         select_lods();
         mouse_prev_x = 0;
         mouse_prev_y = 0;
      }
   }

   private:
   inline float distance3d(const vect& v, const vect& w);
   void select_lods();
   void find_ranges();
   void find_transl_to_origin();
   void change_polygon_mode();

   std::vector< object3dv<vertex> > lods;
   std::vector<float> rules;
   std::vector<vect> positions;
   std::vector<unsigned> selected_lods;

   std::vector< std::pair<vect,vect> > ranges;
   std::vector<vect> transl_to_origin;

   lighting& light;
   int& polygon_mode;

   vect user_pos;
   GLfloat hrot, vrot;
   vect front;
   vect hrotv;
   vect vrotv;

   int mouse_prev_x;
   int mouse_prev_y;

   bool use_lod;

   static const float INFINITY_COORDINATE = 1e8f;
   static const float STEP_LENGTH = 0.1f;
   static const float ONE_PIXEL_VROT = 0.5;
   static const float ONE_PIXEL_HROT = 0.8;
};

#endif
